<?php

namespace App\Containers\AppSection\Questionnaire\UI\API\Transformers;

use App\Containers\AppSection\File\Models\File;
use App\Containers\AppSection\File\UI\API\Transformers\FileTransformer;
use App\Containers\AppSection\Questionnaire\Models\Value;
use App\Ship\Parents\Transformers\Transformer;
use League\Fractal\Resource\Item;

class ValueTransformer extends Transformer
{
    protected array $defaultIncludes = [
        'field'
    ];

    /**
     * @var  array
     */
    protected array $availableIncludes = [

    ];

    public function transform(Value $t): array
    {
        $response = [
            'object' => $t->getResourceKey(),
            'include' => $this->getAvailableIncludes(),
            'id' => $t->getHashedKey(),
            'field_id' => $t->field_id,
            'response_id' => $t->response_id,
            'person' => $t->person,
            'value' => $t->value,

        ];

        if (($t->field->type_key === 'files' || $t->field->type_key === 'file') && !empty($t->value[0])) {
            $files = $this->collection(
                File::whereIn('id', $t->value)->get() ?? [],
                new FileTransformer)->getData();

            $response['files'] = $files;
        }
        return $response;
    }

    public function includeField(Value $t): Item
    {
        return $this->item($t->field, new FieldTransformer);
    }
}
